﻿using System.Data.Entity;
using SmallBusinessKit.DataAccess.Interfaces;

namespace SmallBusinessKit.DataAccess.Infrastructure
{
    public class DatabaseFactory : Disposable, IDatabaseFactory
    {
        #region Properties

        private IDatabaseInitializer<SmallBusinessContext> _ctxInitializer;
        private SmallBusinessContext _dataContext;
        
        #endregion
        
        #region Methods

        public DatabaseFactory(IDatabaseInitializer<SmallBusinessContext> ctxInitializer)
        {
            _ctxInitializer = ctxInitializer;
        }

        public SmallBusinessContext GetContext()
        {
            return _dataContext ?? (_dataContext = new SmallBusinessContext(_ctxInitializer));
        }

        protected override void DisposeCore()
        {
            _dataContext?.Dispose();
        }

        #endregion
    }
}
